package com.ruoyix.im.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyix.common.annotation.Log;
import com.ruoyix.common.annotation.RepeatSubmit;
import com.ruoyix.common.core.controller.BaseController;
import com.ruoyix.common.core.domain.PageQuery;
import com.ruoyix.common.core.domain.R;
import com.ruoyix.common.core.page.TableDataInfo;
import com.ruoyix.common.core.validate.AddGroup;
import com.ruoyix.common.core.validate.EditGroup;
import com.ruoyix.common.enums.BusinessType;
import com.ruoyix.common.helper.LoginHelper;
import com.ruoyix.common.utils.poi.ExcelUtil;

import com.ruoyix.im.domain.ImChatGroup;
import com.ruoyix.im.domain.ImChatGroupUser;
import com.ruoyix.im.domain.bo.ImChatGroupUserBo;
import com.ruoyix.im.domain.vo.GroupUserVO;
import com.ruoyix.im.domain.vo.ImChatGroupUserVo;
import com.ruoyix.im.service.IImChatGroupUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tio.core.Tio;

import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 聊天群成员
 *
 * @author ruoyi
 * @date 2024-05-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/weblog/chatGroupUser")
public class ImChatGroupUserController extends BaseController {

    private final IImChatGroupUserService iImChatGroupUserService;

    /**
     * 查询聊天群成员列表
     */
    @SaCheckPermission("weblog:chatGroupUser:list")
    @GetMapping("/list")
    public TableDataInfo<ImChatGroupUserVo> list(ImChatGroupUserBo bo, PageQuery pageQuery) {
        return iImChatGroupUserService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出聊天群成员列表
     */
    @SaCheckPermission("weblog:chatGroupUser:export")
    @Log(title = "聊天群成员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ImChatGroupUserBo bo, HttpServletResponse response) {
        List<ImChatGroupUserVo> list = iImChatGroupUserService.queryList(bo);
        ExcelUtil.exportExcel(list, "聊天群成员", ImChatGroupUserVo.class, response);
    }

    /**
     * 获取聊天群成员详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("weblog:chatGroupUser:query")
    @GetMapping("/{id}")
    public R<ImChatGroupUserVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(iImChatGroupUserService.queryById(id));
    }

    /**
     * 新增聊天群成员
     */
    @SaCheckPermission("weblog:chatGroupUser:add")
    @Log(title = "聊天群成员", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ImChatGroupUserBo bo) {
        return toAjax(iImChatGroupUserService.insertByBo(bo));
    }

    /**
     * 修改聊天群成员
     */
    @SaCheckPermission("weblog:chatGroupUser:edit")
    @Log(title = "聊天群成员", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ImChatGroupUserBo bo) {
        return toAjax(iImChatGroupUserService.updateByBo(bo));
    }

    /**
     * 删除聊天群成员
     *
     * @param ids 主键串
     */
    @SaCheckPermission("weblog:chatGroupUser:remove")
    @Log(title = "聊天群成员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(iImChatGroupUserService.deleteWithValidByIds(Arrays.asList(ids), true));
    }

    /**
     * 申请加群
     */
    @GetMapping("/enterGroup")
    public R<Void> enterGroup(@RequestParam("id") Long id, @RequestParam(value = "remark", required = false) String remark) {
        return iImChatGroupUserService.enterGroup(id, remark);
    }



    /**
     * 改变群组用户状态
     * <p>
     * 用户状态[-1:审核不通过或者踢出群组，1:审核通过，2:禁言]
     */
    @GetMapping("/changeUserStatus")
    public R<Void> changeUserStatus(@RequestParam("groupId") Long groupId,
                                         @RequestParam("userId") Long userId,
                                         @RequestParam("userStatus") Long userStatus,
                                         @RequestParam("oldUserStatus") Long oldUserStatus) {
        return iImChatGroupUserService.changeUserStatus(groupId, userId, userStatus, oldUserStatus);
    }



    /**
     * 设置群组管理员
     * <p>
     * adminFlag = true 是管理员
     * adminFlag = false 不是管理员
     */
    @GetMapping("/changeAdmin")
    public R<Void> changeAdmin(@RequestParam("groupId") Long groupId,
                                    @RequestParam("userId") Long userId,
                                    @RequestParam("adminFlag") Boolean adminFlag) {
        return iImChatGroupUserService.changeAdmin(groupId, userId, adminFlag);
    }



    /**
     * 退群
     */
    @GetMapping("/quitGroup")
    public R<Void> quitGroup(@RequestParam("id") Long id) {
        return iImChatGroupUserService.getGroupUser(id);
    }



    /**
     * 群管理员查询群用户
     */
    @GetMapping("/getGroupUserByStatus")
    public R<Page<GroupUserVO>> getGroupUserByStatus(@RequestParam(value = "groupId", required = false) Long groupId,
                                                     @RequestParam(value = "userStatus", required = false) Integer userStatus,
                                                     @RequestParam(value = "current", defaultValue = "1") Long current,
                                                     @RequestParam(value = "size", defaultValue = "20") Long size) {
        return iImChatGroupUserService.getGroupUserByStatus(groupId, userStatus, current, size);
    }

    /**
     * 群用户查询群用户
     */
    @GetMapping("/getGroupUser")
    public R<Page<GroupUserVO>> getGroupUser(@RequestParam("groupId") Long groupId,
                                           @RequestParam(value = "current", defaultValue = "1") Long current,
                                           @RequestParam(value = "size", defaultValue = "20") Long size) {
        return iImChatGroupUserService.getPagePoetryResult(groupId, current, size);
    }


}
